<?php

namespace app\api\controller;

use app\common\controller\Api;
header('Content-Type: text/html; charset=utf-8');
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $name = 'booking';
        $config = get_addon_fullconfig($name);
        $img = '';
         foreach ($config as $index => &$item) {
            if ($item['name'] == 'screen_img') {
                $img = $item['value'];
            }
            unset($config[$index]);
        }
        $this->success('请求成功',cdnurl($img, true));
    }
    
   public function str()
    {
        $string = "我是骂人看到去你妈你了卡死萨嘿嘿达奥利给刷卡有病大绿卡马保国的少女拉卡萨马保国几奥利给小可爱女出"; //接收过来的值
        $result =sensitive($string);//调用其他方法
        var_dump($result);die;
    }
   public function  ceshi()
   {
    //   $res=binding(12,14);
      
    //   $res=getParent(12);
       
       $res=parentMoney(100,12);
      var_dump($res);die;
   }
   public function getKefu()
   {
       $uid=$this->request->param("id");//客服ID
       $uid=$uid ? $uid:1;
        if($uid!=1){
            //门店
            $sids=\think\Db::table("rzjd_booking_store_admin")->where(["admin_id"=>$uid])->column("store_ids");
            $shoArr=isset($sids[0]) ? $sids[0]:"";
            $sa["id"]=["in",$shoArr];
            $shops=\think\Db::table("rzjd_booking_store")->where($sa)->field("id,name,avatar,phone")->select();
            
            
            $agentids=[];
            //客服代表
            if($shops){
                foreach ($shops as $suk=>$suv)
                {
                    $usw[] = ['exp',\think\Db::raw("FIND_IN_SET({$suv["id"]},a.store_ids)")];
                    $ladmins=\think\Db::table("rzjd_booking_store_admin")->alias("a")
                     ->join("rzjd_admin b","a.admin_id=b.id","right")
                    ->where($usw)
                    ->field("b.id")->select();
                    
                    if($ladmins){
                        foreach ($ladmins as $lk=>$lv)
                        {
                            array_push($agentids,$lv["id"]);
                        }
                    }
                }
            }
           $agentids=array_unique($agentids);//id去重
            $agw["b.id"]=["in",$agentids];
            $agents=\think\Db::table("rzjd_admin")->alias("b")
                    ->where($agw)
                    ->field("b.id,b.nickname as name,b.avatar,b.email,b.mobile as phone")->select();
        }else{
            //超管
            //店铺
            $shops=[["name"=>\think\Config::get("site.kefuName"),"avatar"=>cdnurl(\think\Config::get("site.kefuHead"), true),"id"=>"0","goods"=>[]]];
            $idsArr=$uid;
            $wa["id"]=["in",$idsArr];
            $agentsArr=[];
            //客服代表
            $agents=\think\Db::table("rzjd_admin")->where($wa)->field("id,nickname as name,avatar,email,mobile as phone")->select();
           
        }
        $newAgents=[];
        if($shops && $agents)
        {
            foreach ($shops as $ks=>&$kv)
            {
                $kv["id"].="shop";
                $kv["avatar"]=cdnurl($kv["avatar"], true);
                if(!isset($kv["goods"])){
                   $kv["goods"]=[];   
                }
                foreach ($agents as $ak=>&$av)
                {
                    $agarr["id"]=$av["id"]."agent";
                    $agarr["name"]=$av["name"];
                    $agarr["password"]="miam";
                    $agarr["avatar"]=cdnurl($av["avatar"], true);
                    $agarr["email"]=$av["email"];
                    $agarr["phone"]=$av["phone"];
                    $agarr["shopId"]=$kv["id"];
                    
                    array_push($newAgents,$agarr);
                }
            }
        }
        //当前客服
       $admin=\think\Db::table("rzjd_admin")->where(["id"=>$uid])->field("id,nickname as name,avatar,email,mobile as phone")->find();//当前客服
       if($admin){
           $admin["id"].="agent";
           $admin["avatar"]=cdnurl($admin["avatar"], true);
       }
       $this->success('请求成功',["shops"=>$shops,"agents"=>$newAgents,"admin"=>$admin]);
   }
   
   
    public function getadmminkefu()
   {
       
            //超管
            //店铺
       $shops=["name"=>\think\Config::get("site.kefuName"),"avatar"=>cdnurl(\think\Config::get("site.kefuHead"), true),"id"=>"0","goods"=>[]];
        
       $this->success('请求成功',["shops"=>$shops]);
   }
    
}
